﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;
using DTO;
using BUS;

namespace SpriteExport
{
    class Program
    {
        static void ExportSrite(StreamReader re)
        {
            StreamWriter wr = null;
            string input = null;

            SpriteDTO sprite = null;
            ASprite aSprite = null;

            List<string> names = new List<string>();
            while ((input = re.ReadLine()) != null)
            {
                if (input.StartsWith("Load"))
                {
                    input = input.Substring(input.IndexOf('"') + 1);
                    string filename = input.Substring(0, input.IndexOf('"'));
                    Console.Out.WriteLine("Exporting... {0}", filename);

                    names.Add(Path.GetFileNameWithoutExtension(filename));
                    sprite = SpriteBUS.Load(filename);
                    aSprite = new ASprite();
                    aSprite.Load(sprite);
                    aSprite.SaveShortData(Path.ChangeExtension(filename, "ssp"), null);
                }
                else if (input.StartsWith("ExportFramesID"))
                {
                    input = input.Substring(input.IndexOf('"') + 1);
                    string cFileName = input.Substring(0, input.IndexOf('"'));
                    input = input.Substring(input.IndexOf('"') + 1);
                    input = input.Substring(input.IndexOf('"') + 1);

                    string preFix = input.Substring(0, input.IndexOf('"'));
                    input = input.Substring(input.IndexOf('"') + 1);
                    input = input.Substring(input.IndexOf('"') + 1);
                    string fileMode = input.Substring(0, input.IndexOf('"'));

                    if (fileMode == "NEW" || wr == null)
                    {
                        wr = new StreamWriter(cFileName);
                    }

                    wr.WriteLine();

                    for (int i = 0; i < sprite.Frames.Count; i++)
                    {
                        string def = "#define   " + preFix;
                        Frame frame = sprite.Frames[i];
                        if (frame.Desc.Length == 0)
                        {
                            def += string.Format("FRAME_INDEX_{0}     {1}", i, i);
                        }
                        else
                        {
                            def += frame.Desc.ToUpper() + "     " + i.ToString();
                        }

                        if (wr != null)
                        {
                            wr.WriteLine(def);
                        }
                    }

                    wr.Flush();
                }
                else if (input.StartsWith("ExportAnimsID"))
                {
                    input = input.Substring(input.IndexOf('"') + 1);
                    string cFileName = input.Substring(0, input.IndexOf('"'));
                    input = input.Substring(input.IndexOf('"') + 1);
                    input = input.Substring(input.IndexOf('"') + 1);

                    string preFix = input.Substring(0, input.IndexOf('"'));
                    input = input.Substring(input.IndexOf('"') + 1);
                    input = input.Substring(input.IndexOf('"') + 1);
                    string fileMode = input.Substring(0, input.IndexOf('"'));

                    if (fileMode == "NEW")
                    {
                        wr = new StreamWriter(cFileName);
                    }

                    for (int i = 0; i < sprite.Animations.Count; i++)
                    {
                        string def = "#define   " + preFix;
                        Animation animation = sprite.Animations[i];
                        if (animation.Desc.Length == 0)
                        {
                            def += string.Format("ANIM_INDEX_{0}     {1}", i, i);
                        }
                        else
                        {
                            def += animation.Desc.ToUpper() + "     " + i.ToString();
                        }

                        if (wr != null)
                        {
                            wr.WriteLine(def);
                        }
                    }

                    wr.Flush();
                }
            }


            //Sprite list
            wr = new StreamWriter("Sprite_Indics.h");

            wr.WriteLine("#ifndef __SPRDEF__H__");
            wr.WriteLine("#define __SPRDEF__H__");
            wr.WriteLine();

            string strDef;
            for (int i = 0; i < names.Count; i++)
            {
                strDef = string.Format("#define     SPRITE_{0}     {1}", names[i].ToUpper(), i);
                wr.WriteLine(strDef);
            }

            strDef = string.Format("#define     SPRITE_NUM     {0}", names.Count);
            wr.WriteLine(strDef);
            wr.WriteLine();
            wr.WriteLine("#endif");
            wr.Flush();

            //*******************************************************
            wr = new StreamWriter("spriteslist.h");

            wr.WriteLine("const char* kSpriteList[]= {");
            for (int i = 0; i < names.Count; i++)
            {
                strDef = string.Format("    \"{0}\",", names[i]);
                wr.WriteLine(strDef);
            }
            wr.WriteLine("};");
            wr.Flush();
        }

        static void ExportTexture(StreamReader re)
        {
            string input = null;


            while ((input = re.ReadLine()) != null)
            {
                if (input.Trim().Length != 0)
                {
                    string filename = input.Trim();
                    Console.Out.WriteLine("Exporting... {0}", filename);

                    try
                    {
                        Bitmap img = new Bitmap(filename);
                        if (img != null)
                        {
                            CFileWriter writer = CFileWriter.Open(Path.ChangeExtension(filename, "bbm"));

                            writer.WriteByte(16);   //RGBA8888

                            int a = Math.Max(img.Width, img.Height);
                            int kSizeW = 1;
                            while (kSizeW < img.Width)
                            {
                                kSizeW *= 2;
                            }

                            int kSizeH = 1;
                            while (kSizeH < img.Height)
                            {
                                kSizeH *= 2;
                            }

                            writer.WriteShort((short)kSizeW);
                            writer.WriteShort((short)kSizeH);
                            byte[] buff = new byte[kSizeW * kSizeH * 4];

                            Array.Clear(buff, 0, buff.Length);

                            int k = 0;
                            for (int j = 0; j < kSizeH; j++)
                                for (int i = 0; i < kSizeW; i++)
                                {
                                    if (j < img.Height && i < img.Width)
                                    {
                                        Color c = img.GetPixel(i, j);

                                        buff[k++] = c.R;
                                        buff[k++] = c.G;
                                        buff[k++] = c.B;
                                        //if (SpriteDTO.Equals(c, transColor))
                                        //{
                                        //    buff[k++] = 0;
                                        //}
                                        //else
                                        {
                                            buff[k++] = c.A;
                                        }
                                    }
                                    else
                                    {
                                        buff[k++] = 0;
                                        buff[k++] = 0;
                                        buff[k++] = 0;
                                        buff[k++] = 0;
                                    }

                                }
                            //int a = Math.Max(img.Width, img.Height);
                            //int kSize = 1;
                            //while (kSize < a)
                            //{
                            //    kSize *= 2;
                            //}

                            //writer.WriteShort((short)kSize);
                            //writer.WriteShort((short)kSize);
                            //byte[] buff = new byte[kSize * kSize * 4];

                            //Array.Clear(buff, 0, buff.Length);

                            //int k = 0;
                            //for (int j = 0; j < kSize; j++)
                            //    for (int i = 0; i < kSize; i++)
                            //    {
                            //        if (j < img.Height && i < img.Width)
                            //        {
                            //            Color c = img.GetPixel(i, j);

                            //            buff[k++] = c.R;
                            //            buff[k++] = c.G;
                            //            buff[k++] = c.B;
                            //            //if (SpriteDTO.Equals(c, transColor))
                            //            //{
                            //            //    buff[k++] = 0;
                            //            //}
                            //            //else
                            //            {
                            //                buff[k++] = c.A;
                            //            }
                            //        }
                            //        else
                            //        {
                            //            buff[k++] = 0;
                            //            buff[k++] = 0;
                            //            buff[k++] = 0;
                            //            buff[k++] = 0;
                            //        }

                            //    }

                            writer.Write(buff);
                            writer.Release();
                        }

                    }
                    catch(Exception )
                    {
                        Console.Out.WriteLine("Erorr... !!!");
                    }
                }

            }
        }
        static void Main(string[] args)
        {
            //StreamReader re = File.OpenText(args[0]);


            StreamReader re = File.OpenText(args[0]);
            if (args.Length == 2 && args[1] == "tex")
            {
                ExportTexture(re);
            }
            else
            {
                ExportSrite(re);
            }

            re.Close();
        }
    }
}
